package com.ecs.server.datadealer;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.List;

import org.hibernate.classic.Session;

import com.ecs.message.ECSMessage;
import com.ecs.message.Illuminance;
import com.ecs.message.Temperature;

public class JudgeRequest {
	
	 Socket s;
	 ECSMessage message;
	
	public JudgeRequest(Socket s, ECSMessage message)
	{
		this.message = message;
		this.s = s;
		Judge(s, message);
	}
	
	private void Judge(Socket s, ECSMessage message)
	{
		String messageType = message.getMessageType();
		String messageMeasure = message.getMeasureType();
		if(messageType.equals("UpdateMessage"))
		{
			if(messageMeasure.equals("Temperature"))
			{
				Session session = ClientManager.factory.openSession();
				session.beginTransaction();
				List search  = session.createQuery("select new Temperature(id,networkId,nodeId,sensorId,value,date) from Temperature " +
						"where id=(select max(id) from Temperature)").list();
				
				ECSMessage replyMessage = new ECSMessage();
				replyMessage.setMessageType("UpdateReply");
				replyMessage.setMeasureType("Temperature");
				replyMessage.setContent(search);
				
				try {
					ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
					oos.writeObject(replyMessage);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

}
